package com.cc.controller;

import com.cc.model.Customer;
import com.cc.response.ServerResponse;
import com.cc.service.ICustomerService;
import com.cc.util.FileUtils;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("customer")
public class CustomerController {

    @Autowired
    private ICustomerService customerService;
    //TODO 一、统一返回
    @RequestMapping("/list")
    @ResponseBody
    public ServerResponse<List<Customer>> list(@RequestParam(value = "page", defaultValue = "1") int page,
            @RequestParam(value = "limit", defaultValue = "2") int limit) {
        // 1.调用service层
        List<Customer> customerList = customerService.list(page, limit);
        Page<Customer> customerPage = (Page<Customer>) customerList;
        // 2.返回数据
        return ServerResponse.successMsg(customerPage.getTotal(), customerList);
    }

    //TODO 二、文件上传
    @RequestMapping(value = "/upload",method= RequestMethod.POST)
    @ResponseBody
    public String fileUpload(MultipartFile pictureFile) throws Exception {
        // 1.原始文件名称
        String pictureFile_name = pictureFile.getOriginalFilename();//
        //检查/Users/mr.chan/Documents/upload/temp/下有没有叫pig.png的文件
        // 2.新文件名称
        String newFileName = UUID.randomUUID().toString()
                + pictureFile_name.substring(pictureFile_name.lastIndexOf("."));
        
        // 3.上传图片
        File uploadPic = new File("/Users/mr.chan/Documents/upload/temp/" + newFileName);
        if (!uploadPic.exists()) {
            uploadPic.mkdirs();
        }
        // 4.向磁盘写文件
        pictureFile.transferTo(uploadPic);
        //5.返回
        return "success";
    }
    //TODO 三、文件下载
    @RequestMapping("/d1")
    @ResponseBody
    public String downloadFile(HttpServletResponse response){
        String fileName="猪.png";
        try {
            fileName = URLEncoder.encode(fileName, "UTF-8");
            //获取页面输出流
            ServletOutputStream outputStream = response.getOutputStream();
            //读取文件
            byte[] bytes = FileUtils.readFileToByteArray(new File("/Users/mr.chan/Documents/upload/temp/pig.png"));
            //向输出流写文件
            //写之前设置响应流以附件的形式打开返回值,这样可以保证前边打开文件出错时异常可以返回给前台
            response.setHeader("Content-Disposition","attachment;filename="+fileName);
            outputStream.write(bytes);
            outputStream.flush();
            outputStream.close();
            return "success";
        } catch (IOException e) {
            return "error";
        }

    }

}
